package com.gpframework.module.ctn.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.ctn.domain.GpCtnOrder;
import com.gpframework.module.ctn.domain.GpPackagesInformation;
import com.gpframework.module.ctn.domain.GpReceiptGoods;
import com.gpframework.module.ctn.domain.GpSackInformation;
import com.gpframework.module.ctn.domain.vo.GpCtnOrderVo;
import com.gpframework.module.ctn.domain.vo.GpFormVo;
import com.gpframework.module.ctn.domain.vo.GpReceiptGoodsExcel;
import com.gpframework.module.ctn.domain.vo.GpReceiptGoodsVo;
import com.gpframework.module.ctn.mapper.GpReceiptGoodsMapper;
import com.gpframework.module.ctn.service.IGpCtnOrderService;
import com.gpframework.module.ctn.service.IGpPackagesInformationService;
import com.gpframework.module.ctn.service.IGpReceiptGoodsService;
import com.gpframework.module.ctn.service.IGpSackInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName GpReceiptGoods
 * @Author gpframework
 * @Date 2024-01-08
 * @Version 1.0.0
 * @Description 录入订单Service业务层处理
 */
@Service
public class GpReceiptGoodsServiceImpl extends ServiceImpl<GpReceiptGoodsMapper, GpReceiptGoods> implements IGpReceiptGoodsService {


    @Autowired
    private IGpPackagesInformationService iGpPackagesInformationService;
    @Autowired
    private IGpCtnOrderService iGpCtnOrderService;
    @Autowired
    private IGpSackInformationService iGpSackInformationService;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2024-01-08
     * @Param [lqw]
     * @Return List<GpReceiptGoodsVo>
     */
    @Override
    public List<GpReceiptGoodsVo> listVo(QueryWrapper<GpReceiptGoods> lqw) {
        List<GpReceiptGoodsVo> list = this.baseMapper.listVo(lqw);
        for (GpReceiptGoodsVo vo : list) {
            //里面有2层
            vo.setOrderPackLoadList(iGpPackagesInformationService.listVoed(new QueryWrapper<GpPackagesInformation>().eq("gp.del_status", YesNoEnum.NO.getKey())));
            vo.setOrderLoadList(iGpCtnOrderService.listVo(new QueryWrapper<GpCtnOrder>().eq("gp.del_status", YesNoEnum.NO.getKey())));
            vo.setGpPackagesInformationList(iGpPackagesInformationService.listVo(new QueryWrapper<GpPackagesInformation>().eq("gp.receipt_id", vo.getId()).eq("gp.del_status", YesNoEnum.NO.getKey())));
            vo.setGpSackInformationList(iGpSackInformationService.listVo(new QueryWrapper<GpSackInformation>().eq("gp.receipt_id", vo.getId()).eq("gp.del_status", YesNoEnum.NO.getKey())));
            vo.setUngpPackagesInformationList(iGpPackagesInformationService.listVo(new QueryWrapper<GpPackagesInformation>().isNull("gp.ctn_order_id").eq("gp.del_status", YesNoEnum.NO.getKey())));
            vo.setUngpSackInformationList(iGpSackInformationService.listVo(new QueryWrapper<GpSackInformation>().isNull("gp.ctn_order_id").eq("gp.del_status", YesNoEnum.NO.getKey())));
            for (GpCtnOrderVo gpCtnOrderVo : vo.getOrderLoadList()) {
                gpCtnOrderVo.setPackagesInformationList(iGpPackagesInformationService.listVo(new QueryWrapper<GpPackagesInformation>().eq("gp.ctn_order_id", gpCtnOrderVo.getId()).eq("gp.receipt_id", vo.getId()).eq("gp.del_status", YesNoEnum.NO.getKey())));
                gpCtnOrderVo.setSackInformationList(iGpSackInformationService.listVo(new QueryWrapper<GpSackInformation>().eq("gp.ctn_order_id", gpCtnOrderVo.getId()).eq("gp.receipt_id", vo.getId()).eq("gp.del_status", YesNoEnum.NO.getKey())));
            }
        }
        return list;
    }

    @Override
    public GpReceiptGoodsVo getVoById(Long id) {
        QueryWrapper<GpReceiptGoods> lqw  = new QueryWrapper<>();
        lqw.eq("gp.id",id);
        List<GpReceiptGoodsVo> list = this.listVo(lqw);
        return list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public List<GpReceiptGoodsExcel> gpReceiptGoodsImport(List<GpReceiptGoodsExcel> excelList, Long brandId, Long storeId) {
        //导入失败的列表
        List<GpReceiptGoodsExcel> failureList = new ArrayList<>();
        //录入订单
        GpReceiptGoods gpReceiptGoods;

        for (int i = 0; i < excelList.size(); i++) {
            GpReceiptGoodsExcel excel = excelList.get(i);
            if (StringUtils.isNull(excel)) {
                excel = new GpReceiptGoodsExcel();
                excel.setSubscript(i+1);
                excel.setMsg("读取数据失败");
                failureList.add(excel);
                continue;
            }
            gpReceiptGoods = Convert.convert(new TypeReference<GpReceiptGoods>() {}, excel);
            this.save(gpReceiptGoods);
        }
        return failureList;
    }

    @Override
    public List<GpFormVo> customerStatistics(QueryWrapper<GpReceiptGoods> lqw) {
        return this.baseMapper.customerStatistics(lqw);
    }

    @Override
    public List<GpFormVo> supplierStatistics(QueryWrapper<GpReceiptGoods> lqw) {
        return this.baseMapper.supplierStatistics(lqw);
    }


}
